package com.niit.session19;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JDBCDemo {
	public static void main(String[] args) {
//		1.下载驱动程序，在项目根目录创建lib文件夹，把驱动程序拖进去，有右键选择build path -》add to build path
		
//		2.加载驱动程序
		Connection conn = null;
		Statement statement =null;
		ResultSet rs = null;
			try {
				Class.forName("com.mysql.cj.jdbc.Driver");
//			3.获取数据库链接
				String url ="jdbc:mysql://localhost/student_2021";
				String username ="root";
				String password ="root";
				conn = DriverManager.getConnection(url, username, password);
//			4.执行sql语句
				statement = conn.createStatement();
				rs = statement.executeQuery("select * from student where batch = 1");
				while (rs.next()) {
					System.out.println(rs.getString("sid")+"---"+rs.getString("name"));
				}
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			} catch (SQLException e) {
				e.printStackTrace();
			}finally {
				try {
					conn.close();
					statement.close();
					rs.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			
		
	}

}
